package main

import (
	"Dome/block"
	"fmt"
)

func main() {
	//创建区块
	block1 := block.NewBlock([]byte("第一个区块"), nil)
	fmt.Println(string(block1.Data))

	block2 := block.NewBlock([]byte("第二个区块"), block1.Hash)
	fmt.Println(string(block2.Data))

	bc,err := block.NewBlockChain([]byte("创世区块"))
	if err != nil{
		defer bc.DB.Close()
		fmt.Println(err.Error())
		return
	}

	//向区块链里添加新区块 hello
	err = bc.AddBlock([]byte("hello"))
	if err != nil{
		fmt.Println("添加失败")
	}else{
		fmt.Println("添加成功")
	}

	iterator:= bc.Iterator()
	for{
		if iterator.HasNext(){
			block, err := iterator.Next()
			if err != nil{
				fmt.Println(err.Error())
				break
			}
			fmt.Println(string(block.Data))
		}else {
			break
		}
	}

	//iterator := bc.Iterator()
	//bloc1, err := iterator.Next()
	//fmt.Println(string(bloc1.Data))
	//bloc2,err := iterator.Next()
	//fmt.Println(string(bloc2.Data))

	fmt.Println(bc.LastHash)
	//bc.AddBlock([]byte("hello1"))
	//fmt.Println(string(bc.Blocks[1].Data))
	//for _,block :=range bc.Blocks{
	//	fmt.Printf("prevHash %x\n",block.PrevHash)
	//	fmt.Printf("data %s\n", block.Data)
	//	fmt.Printf("hash %x\n",block.Hash)
	//}

	//fmt.Println(bc.Blocks[1].Nonce)
	//fmt.Println("创世区块的nonce",bc.Blocks[0].Nonce)
}
